package com.heima.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.AuthorService;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service("AuthorService")
public class AuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements AuthorService {
    @Autowired
    ApAuthorMapper authorMapper;

    /**
     * 根据Id查询作者信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult findByUserId(Integer id) {
        if (id == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        ApAuthor author = authorMapper.selectById(id);

        return ResponseResult.okResult(author);
    }

    /**
     * 保存作者
     *
     * @param author
     * @return
     */
    @Override
    public ResponseResult saveAuthor(ApAuthor author) {
        if (author == null)
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);

        author.setCreatedTime(new Date());
        authorMapper.updateById(author);
        return ResponseResult.okResult();
    }
}
